﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;


namespace Sistema_de_Taller.SDT_DAL.Utilities
{
    public class WorkOrder_DAL
    {
        public UInt32 WorkOrderInsert(Common_Files.Definitions.WorkOrder WorkOrder)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlCommand Command = new SqlCommand(@"insert into WorkOrder (WorkOrder_Type, WorkOrder_Account, WorkOrder_Entry, WorkOrder_Close, WorkOrder_State) 
                                                                values ('" + WorkOrder.WorkOrder_Type + "', '" + WorkOrder.WorkOrder_Account + "','" + WorkOrder.WorkOrder_Entry.ToShortDateString() + "','" + WorkOrder.WorkOrder_Close.ToShortDateString() + "', '" + WorkOrder.WorkOrder_State + "'); SET @ID=SCOPE_IDENTITY()", Connection);
            //Obtengo el ID del WorkOrder agregado
            SqlParameter par = new SqlParameter();
            par.ParameterName = "@ID";
            par.Size = 4;
            par.Direction = ParameterDirection.Output;
            Command.Parameters.Add(par);
            Command.ExecuteNonQuery();
            Connection.Close();
            return (Convert.ToUInt32(par.Value));
        }


        public DataSet WorkOrderLoad()
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select WorkOrder_Type as 'Tipo', WorkOrder_Account as 'Cuenta', WorkOrder_Entry as 'Ingreso', WorkOrder_Close as 'Cierre', WorkOrder_State as 'Estado' from WorkOrder", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);
        }

        public DataSet WorkOrderLoadID(UInt32 WorkOrderID)
        {
            //EL @ es para dividir una línea de código en 2
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(@"Select WorkOrder_Type as 'Tipo', WorkOrder_Account as 'Cuenta', WorkOrder_Entry as 'Ingreso', WorkOrder_Close as 'Cierre', WorkOrder_State as 'Estado' from WorkOrder  where WorkOrder_ID = '" + WorkOrderID + "'", Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);
        }

        public void WorkOrderUpdate(Common_Files.Definitions.WorkOrder WorkOrder)
        {
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlCommand Command = new SqlCommand("update WorkOrder set WorkOrder_Type= '" + WorkOrder.WorkOrder_Type + "' , WorkOrder_Account = '" + WorkOrder.WorkOrder_Account + "', WorkOrder_Entry = '" + WorkOrder.WorkOrder_Entry.ToShortDateString() + "' , WorkOrder_Close = '" + WorkOrder.WorkOrder_Close.ToShortDateString() + "', WorkOrder_State = '" + WorkOrder.WorkOrder_State + "' where WorkOrder_ID = '" + WorkOrder.WorkOrder_ID + "' ", Connection);
            Common_Files.Connection.CloseConecction(Connection, Command);
        }

        public void WorkOrderDelete()
        {
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            //  SqlCommand Command = new SqlCommand("delete from WorkOrder where mov_codigo = '" + DgvDatos.SelectedRows[0].Cells[0].Value.ToString() + "'", Connection);
            //  CloseConecction(Connection, Command);
        }

        public DataSet WorkOrderSearch(String Parameter, String Column)
        {
            String query = string.Format("Select WorkOrder_Type as 'Tipo', WorkOrder_Account as 'Cuenta', WorkOrder_Entry as 'Ingreso', WorkOrder_Close as 'Cierre', WorkOrder_State as 'Estado' from WorkOrder where {0} like '%" + Parameter + "%'", Column);
            SqlConnection Connection = Common_Files.Connection.OpenConecction();
            SqlDataAdapter Command = new SqlDataAdapter(query, Connection);
            DataSet DataSet = new DataSet();
            Command.Fill(DataSet);
            return (DataSet);
        }

    }
}